package com.bilin.coin.network.callback;


import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bilin.coin.BuildConfig;
import com.bilin.coin.App;
import com.bilin.coin.network.RequestUtils;
import com.bilin.coin.network.bean.ModelBean;
import com.bilin.coin.utils.DialogUtils;
import com.google.gson.JsonSyntaxException;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;


import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 *
 */

public abstract class StringCallBack<T> implements Observer<String> {


    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(String result) {
        try {
            Class<T> clz = (Class<T>) (((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]);

            if (clz.toString().contains("String")) {

                if (!result.equals("") && result != null) {
                    if (result.contains("code")) {
                        ModelBean modelBean = RequestUtils.getGson().fromJson(result, ModelBean.class);
                        if (modelBean.getCode().equals("200")) {
                            onCallBackNext((T) result);
                        } else if (modelBean.getCode().equals("500")) {
                            DialogUtils.dismissDialogLoading();
                            //该位置跳转登录

                            ARouter
                                    .getInstance()
                                    .build("/confirm/verification")
                                    .withString("loginMsg", "msg")
                                    //.withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                                    .navigation(App.getApp());
                        } else {
                            try {
                                JSONObject data = new JSONObject(result);
                                if (!TextUtils.isEmpty(modelBean.getMsg())) {
                                    onCallBackError(new Throwable(modelBean.getCode(), new Throwable(data.getString("msg"))));
                                } else {
                                    onCallBackError(new Throwable(modelBean.getCode()));
                                }
                                //onCallBackError(new Throwable(modelBean.getCode(), new Throwable(data.getString("data"),new Throwable(data.getString("msg")))));
                                //onCallBackError(new Throwable(modelBean.getCode(), new Throwable(data.getString("data"))));
                            } catch (JSONException e) {
                                onCallBackError(new Throwable(modelBean.getCode()));
                                e.printStackTrace();
                            }

                           /* if (!TextUtils.isEmpty(modelBean.getMsg())) {
                                onCallbackErrorWithMsg(modelBean.getMsg());
                            }*/

                        }
                    } else {
                        onCallBackNext((T) result);
                    }

                }


            } else {
                T t = null;
                try {
                    t = RequestUtils.getGson().fromJson(result, clz);
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
                if (t == null) {
                    onCallBackError(new Throwable("2018"));
                    //throw new NullPointerException();
                }
                if (!result.equals("") && result != null) {
                    ModelBean modelBean = RequestUtils.getGson().fromJson(result, ModelBean.class);
                    if (modelBean.getCode() != null && !modelBean.getCode().equals("")) {
                        if (modelBean.getCode().equals("200")) {
                            onCallBackNext(RequestUtils.getGson().fromJson(result, clz));
                        } else if (modelBean.getCode().equals("500")) {
                            DialogUtils.dismissDialogLoading();
                            //该位置跳转登录


                            ARouter
                                    .getInstance()
                                    .build("/confirm/verification")
                                    .withString("loginMsg", "msg")
                                    //.withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                                    .navigation(App.getApp());
                        } else {
                            if (!TextUtils.isEmpty(modelBean.getMsg())) {
                                onCallBackError(new Throwable(modelBean.getCode(),new Throwable(modelBean.getMsg())));
                                //onCallbackErrorWithMsg(modelBean.getMsg());
                            } else {
                                onCallBackError(new Throwable(modelBean.getCode()));
                            }
                        }
                    } else {
                        try {
                            onCallBackNext(RequestUtils.getGson().fromJson(result, clz));
                        } catch (Exception e) {
                            onCallBackError(new Throwable("2018"));
                        }

                    }

                } else {
                    onCallBackError(new Throwable("2018"));
                }

            }
        } catch (JSONException e) {
            onCallBackError(new Throwable("2018"));
            e.printStackTrace();
        }

    }

  /*  @Override
    public void onError(Throwable e) {

        if (e != null && e.getMessage() != null && !e.getMessage().equals("")) {
            onCallBackError(e);
        } else {
            onCallBackError(new Throwable("2018"));
        }


    }*/

    @Override
    public void onError(Throwable e) {
        if (e != null && e.getMessage() != null && !e.getMessage().equals("")) {
            //onCallBackError(e);
            if (BuildConfig.DEBUG) {
                onCallBackError(new Throwable(e.getMessage()));
            } else {
                onCallBackError(new Throwable("网络开小差了"));
            }
        } else {
            if (BuildConfig.DEBUG) {
                onCallBackError(new Throwable("e is null"));
            } else {
                onCallBackError(new Throwable("网络开小差了"));
            }
        }
    }

    @Override
    public void onComplete() {

    }

    public abstract void onCallBackNext(T result) throws JSONException;

    public abstract void onCallBackError(Throwable e);

    public void onCallbackErrorWithMsg(String errorMsg) {

    }

}
